package com.jxwifi.cloud.quickclean.app;


import android.content.Context;

import com.idroid.async.AsyncWorker;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.bean.PrefKey;
import com.jxwifi.cloud.quickclean.okhttp.AuzHttp;

import cn.trinea.android.common.util.PreferencesUtils;
import cn.trinea.android.common.util.StringUtils;


/**
 * 用户信息管理
 */
public class AccountMgr extends PreferencesUtils {

    public Context mcontext;

    /**
     * 用户登陆成功时触发
     *
     * @param context
     */
    public static void loginSuccess(final Context context) {
        AsyncWorker.execute(new Runnable() {
            @Override
            public void run() {
                ResetLogin(context, true);
            }
        });
    }

    /**
     * 重置当前登录状态
     */
    public static void ResetLogin(Context context, boolean isLogin) {
        if (!isLogin)
            putString(context, Const.ID, null);
        putBoolean(context, Const.IsLogin, isLogin);
    }

    /**
     * 是否登录
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        String mid = getid(context);
        if (!StringUtils.isEmpty(mid)) {
            ResetLogin(context, true);
        }
        return getBoolean(context, Const.IsLogin);
    }

    /**
     * 注销时的操作
     *
     * @param context
     */
    public static void logout(final Context context) {
        ResetLogin(context, false);
        AsyncWorker.execute(new Runnable() {
            @Override
            public void run() {
                //退出登录token清除，
                AuzHttp.KJB_APP_Token = "";
                //EventBus.getDefault().post(Boolean.valueOf(false), ETag.ETag_UserLogin);//登录发生改变的时候，如果那个页面的数据需要操作，可以调这个方法
                putString(context, PrefKey.SelCarId, null);
                removeUser(context);
            }
        });
    }

    //用户Code
    public static String getCode(Context context) {
        return getString(context, Const.CUSTOMER_CODE);
    }

    //获取id
    public static String getid(Context context) {
        return getString(context, Const.ID);
    }

    //获取上次保存的手机号
    public static String getLastphone(Context context) {
        return getString(context, Const.LAST_TIME_NAME);
    }

    //保存这次的手机号
    public static void putLastPhone(Context context, String lastname) {
        putString(context, Const.LAST_TIME_NAME, lastname);
    }

    //性别
    public static String getSex(Context context) {
        return getString(context, Const.SEX);
    }

    //token
    public static String gettoken(Context context) {
        return getString(context, Const.TOKEN);
    }

    //token
    public static void puttoken(Context context, String token) {
        putString(context, Const.TOKEN, token);
    }

    //名称
    public static String getName(Context context) {
        return getString(context, Const.NICK_NAME);
    }

    public static void putName(Context context, String name) {
        putString(context, Const.NICK_NAME, name);
    }

    //头像
    public static String getHeadImg(Context context) {
        return getString(context, Const.HEAD_IMG);
    }

    public static void putHeadImg(Context context, String headImg) {
        putString(context, Const.HEAD_IMG, headImg);
    }

    //电话
    public static String gettell(Context context) {
        return getString(context, Const.TELL);
    }

    //电话
    public static void puttell(Context context, String tell) {
        putString(context, Const.TELL, tell);
    }
    //会员等级名称
    public static String getGradeName(Context context) {
        return getString(context, Const.GRADE_NAME);
    }
    public static void putGradeName(Context context, String gradeName) {
        putString(context, Const.GRADE_NAME, gradeName);
    }

    //会员等级
    public static String getGradeVip(Context context) {
        return getString(context, Const.GRADE_VIP);
    }
    public static void putGradeVip(Context context, String gradeVip) {
        putString(context, Const.GRADE_VIP, gradeVip);
    }

    //订单超时提醒
    public static int getOrderTimeOut(Context context) {
        return getInt(context, Const.ORDER_TIMEOUT);
    }
    public static void putOrderTimeOut(Context context, int orderTimeOut) {
        putInt(context, Const.ORDER_TIMEOUT, orderTimeOut);
    }


    //密码
    public static String getpassword(Context context) {
        return getString(context, Const.PASSWORD);
    }

    public static void putUser(Context context, InfoBean us, boolean IsPassword) {
        putString(context, Const.ID, us.getId());
        putString(context, Const.CUSTOMER_CODE, us.getCustomerCode());
        putString(context, Const.NICK_NAME, us.getNickname());
        putString(context, Const.INVITE_CODE, us.getInviteCode());
        putString(context, Const.TELL, us.getTell());
        putString(context, Const.PASSWORD, us.getPassword());
        putString(context, Const.CREATE_TIME, us.getCreateTime());
        putString(context, Const.UPDATE_TIME, us.getUpdateTime());
        putBoolean(context, Const.DELETEED, us.isDeleted());
        putString(context, Const.HEAD_IMG, us.getHeadImg());
        putInt(context, Const.ORDER_COUNTS, us.getOrderCounts());
        putString(context, Const.SEX, us.getSex());
        putString(context, Const.IS_FREEZE, us.getIsFreeze());
        putString(context, Const.WEIXIN_OPEN_ID, us.getWeixinOpenid());
        putString(context, Const.SEEION_KEY, us.getSessionKey());
        putString(context, Const.LAST_LOGIN_TIME, us.getLastLoginTime());
        putString(context, Const.LAST_LOGIN_IP, us.getLastLoginIp());
        putString(context, Const.LAST_ORDER_TIME, us.getLastOrderTime());
        putString(context, Const.FIRST_ORDER_TIME, us.getFirstOrderTime());
        putString(context, Const.STATUS, us.getStatus());
        putString(context, Const.SERVICE_CODE, us.getServiceCode());
        putString(context, Const.REGIS_TYPE, us.getRegistType());
        putString(context, Const.APP_ID, us.getAppId());
        putString(context, Const.DATE_STR, us.getDateStr());
        putString(context, Const.DATE_VALUE, us.getDateValue());

    }

    public static void removeUser(Context context) {
        putString(context, Const.ID, null);
        putString(context, Const.CUSTOMER_CODE, null);
        putString(context, Const.NICK_NAME, null);
        putString(context, Const.INVITE_CODE, null);
        putString(context, Const.TELL, null);
        putString(context, Const.PASSWORD, null);
        putString(context, Const.CREATE_TIME, null);
        putString(context, Const.UPDATE_TIME, null);
        putString(context, Const.DELETEED, null);
        putString(context, Const.HEAD_IMG, null);
        putString(context, Const.ORDER_COUNTS, null);
        putString(context, Const.SEX, null);
        putString(context, Const.IS_FREEZE, null);
        putString(context, Const.WEIXIN_OPEN_ID, null);
        putString(context, Const.SEEION_KEY, null);
        putString(context, Const.LAST_LOGIN_TIME, null);
        putString(context, Const.LAST_LOGIN_IP, null);
        putString(context, Const.LAST_ORDER_TIME, null);
        putString(context, Const.FIRST_ORDER_TIME, null);
        putString(context, Const.STATUS, null);
        putString(context, Const.SERVICE_CODE, null);
        putString(context, Const.SYS_AREA_CODE, null);
        putString(context, Const.REGIS_TYPE, null);
        putString(context, Const.APP_ID, null);
        putString(context, Const.DATE_STR, null);
        putString(context, Const.DATE_VALUE, null);
    }

    public static final class Const {
        //token
        public final static String TOKEN = "token";
        //是否登录
        public final static String IsLogin = "IsLogin";
        //id
        public final static String ID = "id";
        //
        public final static String CUSTOMER_CODE = "customerCode";
        //名称
        public final static String NICK_NAME = "nickname";
        //邀请码
        public final static String INVITE_CODE = "inviteCode";
        //电话
        public final static String TELL = "tell";
        //密码
        public final static String PASSWORD = "password";
        //创建时间
        public final static String CREATE_TIME = "createTime";
        //修改时间
        public final static String UPDATE_TIME = "updateTime";
        //删除
        public final static String DELETEED = "deleted";
        //头像
        public final static String HEAD_IMG = "headImg";
        //订单数
        public final static String ORDER_COUNTS = "orderCounts";
        //性别
        public final static String SEX = "sex";
        //是否冻结
        public final static String IS_FREEZE = "isFreeze";
        //微信ID
        public final static String WEIXIN_OPEN_ID = "weixinOpenid";
        //微信的key
        public final static String SEEION_KEY = "sessionKey";
        //最后登录时间
        public final static String LAST_LOGIN_TIME = "lastLoginTime";
        //最后登录IP
        public final static String LAST_LOGIN_IP = "lastLoginIp";
        //最后下单时间
        public final static String LAST_ORDER_TIME = "lastOrderTime";
        //首次下单时间
        public final static String FIRST_ORDER_TIME = "firstOrderTime";
        //用户状态
        public final static String STATUS = "status";
        //服务编码
        public final static String SERVICE_CODE = "serviceCode";
        //系统区域编码
        public final static String SYS_AREA_CODE = "sysAreaCode";
        //
        public final static String REGIS_TYPE = "registType";
        //
        public final static String APP_ID = "appId";
        //
        public final static String DATE_STR = "dateStr";
        //
        public final static String DATE_VALUE = "dateValue";

        //用户上次登录保存的手机号
        public final static String LAST_TIME_NAME = "LastTimeName";

        public final static String GRADE_NAME ="gradeName";

        public final static  String GRADE_VIP="gradeVip";
        //订单超时时间/分
        public final static  String ORDER_TIMEOUT="orderTimeOut";

    }

}
